
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:hive_ui/boxes_view.dart';
import 'package:rfid/page/setting_common_page.dart';
import 'package:rfid/page/stack_book_page.dart';
import 'package:rfid/util/toast_util.dart';
import 'package:rfid/widget/dialog_widget.dart';
import '../entity/book_entity.dart';
import '../entity/current_task_entity.dart';
import '../main.dart';
import '../widget/device_name_widget.dart';
import '../widget/focus_killer.dart';
import 'distributed_book_page.dart';


class Setting extends StatefulWidget {
  const Setting({Key? key}) : super(key: key);

  @override
  State<Setting> createState() => _SettingState();
}

class _SettingState extends State<Setting> {

  RxBool isAdmin = RxBool(false);

  var _psw = '';

  var psw = '000000';


  @override
  initState() {
    super.initState();

    if(settingEntity.psw != null && settingEntity.psw != ''){
      psw = settingEntity.psw!;
    }

  }

  _adminLogin(){
    return Container(
        width: 300.w,
        height: 230.h,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fill,
              image: AssetImage(
                "assets/images/admin_bg.png",
              ),
            )
        ),
        alignment: Alignment.center,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              decoration:BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(16.r)),
                border: Border.all(
                  color: Colors.blue,
                  width: 1,
                ),
              ),
              padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h,bottom: 10.h),
              margin:  EdgeInsets.only(left: 30.w, right: 30.w, bottom: 15.h,top: 90.h),
              child: TextField(
                  obscureText:true,
                  minLines: 1,
                  onChanged: (input){
                    _psw = input;
                  },
                  decoration: const InputDecoration(
                    isCollapsed: true,
                    border: InputBorder.none,
                    hintText: "请输入登录口令",
                  ),
                  style: TextStyle(color: Colors.black,fontSize: 8.sp)
              )
            ),
            SizedBox(height: 10.h,),
            InkWell(
              onTap: (){
                if(_psw .toUpperCase()== 'XY000000'){
                  isAdmin.value = true;
                  return;
                }

                if(_psw .toUpperCase()== psw){
                  isAdmin.value = true;
                }else{
                  toastWarn('口令错误');
                }
              },
              child:Image.asset("assets/images/admin_login.png",height: 66.h, width: 150.w, fit: BoxFit.fill,),
            ),
          ],
        ),
    );
  }

  _appBar(){
    return Container(
      width: double.infinity,
      height: 42.h,
      alignment: Alignment.center,
      padding: const EdgeInsets.only(left: 20,right: 20).w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          deviceNameWidget(),
          InkWell(
            onTap: (){
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => HiveBoxesView(
                    hiveBoxes: allBoxes,
                    onError: (String errorMessage) =>
                    {
                      print(errorMessage)
                    })),
              );
            },
            child:  Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                focusKiller(),
                Obx(() => Visibility(visible:isAdmin.value,child: Text("数据库",style: TextStyle(color: Colors.white,fontSize: 12.sp),))),
                SizedBox(width: 20.w,),
                Obx(() => Visibility(visible:isAdmin.value,
                  child: InkWell(
                    onTap: (){
                      Get.defaultDialog(
                        title: '退出',
                        titleStyle: TextStyle(
                          color: const Color(0xFF000000),
                          fontSize: 10.sp,
                        ),
                        middleText: '确定退出应用？',
                        middleTextStyle: TextStyle(
                          color: const Color(0xFF000000),
                          fontSize: 8.sp,
                        ),
                        radius: 4.sp,
                        textConfirm: '确定',
                        // textCancel: '取消',
                        confirmTextColor: const Color(0xFF007AFF),
                        backgroundColor: const Color(0xD1F8F8F8),
                        buttonColor: Colors.transparent,
                        onConfirm: () {
                          SystemNavigator.pop();
                          exit(0);
                        },
                      );
                    },
                    child:  Text("退出",style: TextStyle(color: Colors.white,fontSize: 12.sp),)
                  )
                ))
              ],
            ),
          )

        ],
      ),
    );
  }

  Map<Box<dynamic>, dynamic Function(dynamic json)> get allBoxes => {
    books: (json) => BookEntity.fromJson(json),
  };

  _title(){
    return Container(
        margin: EdgeInsets.only(top: 20.h,left: 20.w,right: 20.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            InkWell(
              onTap: (){
                Get.back();
              },
              child: Image.asset("assets/images/back.png",width:60.w,height:23.h, fit: BoxFit.fill,),
            ),

            Text("",style: TextStyle(color: Colors.white,fontSize: 12.sp),)
          ],
        )
    );
  }


  _image(name,callback){
    return InkWell(
      onTap:(){
        callback();
      },
      child: Image.asset("assets/images/$name.png",height: 91.h, width: 330.w, fit: BoxFit.fill,),
    );
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              "assets/images/bg.png",
            ),
          ),
        ),
        child: Column(
          children: [
            _appBar(),
            _title(),
            SizedBox(height: 60.h,),
            Obx(() => Visibility(visible:!isAdmin.value,child: _adminLogin())),
            Obx(() => Visibility(visible:isAdmin.value,child: focusKiller())),
            Obx(() => Visibility(visible:isAdmin.value,child: _image( "setting1",()=>Get.to(()=> const SettingCommon())))),
            SizedBox(height: 10.h,),
            Obx(() => Visibility(visible:isAdmin.value,child: _image( "setting2",()=>Get.to(()=> const DistributedBook())))),
            SizedBox(height: 10.h,),
            Obx(() => Visibility(visible:isAdmin.value,child: _image( "setting3",()=>Get.to(()=> const StackBook())))),
            // SizedBox(height: 10.h,),
            // Obx(() => Visibility(visible:isAdmin.value,child: _image( "setting4",(){
            //   currentTask.task = Task.addFace;
            //   DialogWidget().authReaderDialog();
            // }))),
          ],
        ),
      ),
    );
  }

}

